package com.example.zzl.view.fragment.CartFragment;

import android.content.DialogInterface;
import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AlertDialog;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.zzl.R;
import com.example.zzl.adapter.CartAdapter.CartLvAdapter;
import com.example.zzl.app.MyApplication;
import com.example.zzl.databinding.FragmentCartBinding;
import com.example.zzl.gen.CartDao;
import com.example.zzl.gen.DaoMaster;
import com.example.zzl.model.bean.Cart;
import com.example.zzl.utils.GreenDaoManager;
import com.example.zzl.view.BaseFragment;
import com.example.zzl.view.activity.MainActivity;
import com.example.zzl.view.activity.ShoppingCatActivity.PayDemoActivity;
import com.example.zzl.view.activity.UserActivity.LoginActivity;

import java.util.ArrayList;

public class CartFragment extends BaseFragment implements View.OnClickListener,CompoundButton.OnCheckedChangeListener{

    private FragmentCartBinding binding;
    private CheckBox checkBox;
    private ListView lv;
    private RelativeLayout rl_2;
    private TextView tv;
    private CartDao cartDao;
    private ArrayList<Cart> list;
    private CartLvAdapter adapter;
    public static CartFragment newInstance(String data) {


        Bundle args = new Bundle();
        args.putString("data", data);
        CartFragment fragment = new CartFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        cartDao = GreenDaoManager.getInstance(getActivity()).getNewSession().getCartDao();
        list = new ArrayList<>();

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        binding = DataBindingUtil.inflate(inflater,R.layout.fragment_cart, container, false);
        checkBox = binding.ckAll;
        lv = binding.lvCart;
        rl_2 = binding.rlNo;
        tv = binding.tvNo;
        binding.setMyClick(this);
        checkBox.setOnCheckedChangeListener(this);
        return binding.getRoot();

    }

    private void loadData(){
        list = (ArrayList<Cart>) cartDao.loadAll();
        if(list.size()>0) {
            //Log.e("data",list.size()+"");
            //adapter.notifyDataSetChanged();
            adapter = new CartLvAdapter(getActivity(), list);
            lv.setAdapter(adapter);



        }
    }


    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (!hidden) {
            loadData();


            if(MyApplication.nichName!=null){
                lv.setVisibility(View.VISIBLE);
                rl_2.setVisibility(View.GONE);
                lv.setEmptyView(tv);
                //tv.setVisibility(View.VISIBLE);
            }else{
                rl_2.setVisibility(View.VISIBLE);
                lv.setVisibility(View.GONE);
                tv.setVisibility(View.GONE);
            }
        }
    }

    @Override
    public void onClick(View view) {
        switch(view.getId()){
            case R.id.btn_go://去登录
                startActivityForResult(new Intent(getActivity(), LoginActivity.class),888);
                break;
            case R.id.tv_jiesuan://结算
                AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
                builder.setTitle("确认支付价格").setMessage("订单总价："+ MainActivity.sum).setNegativeButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        startActivityForResult(new Intent(getActivity(), PayDemoActivity.class),555);
                    }
                }).setPositiveButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                    }
                }).setCancelable(false).create().show();
                break;
            case R.id.tv_edit://编辑
                break;
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode == 888 && resultCode == 900){
            lv.setVisibility(View.VISIBLE);
            rl_2.setVisibility(View.GONE);
            lv.setEmptyView(tv);
            //tv.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
        if(b){
            adapter.setCheck(true);
            adapter.notifyDataSetChanged();
            //Toast.makeText(getActivity(), "true", Toast.LENGTH_SHORT).show();
        }else{
            MainActivity.sum = 0;
            adapter.setCheck(false);
            adapter.notifyDataSetChanged();
           //Toast.makeText(getActivity(), "false", Toast.LENGTH_SHORT).show();
        }
    }

}
